<template>
  <div class="mainContainer">
    <!-- Search start -->
    <el-form
      :inline="true"
      size="small"
      :model="queryParams"
      class="demo-form-inline searchContainer"
    >
      <el-form-item label="SQL编号: ">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入SQL编号..."
          @keyup.enter="onSearch"
        />
      </el-form-item>
      <el-form-item label="SQL类型:">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择SQL类型..."
          collapse-tags
          clearable
          style="width: 150px"
        >
          <el-option label="查询" value="1"></el-option>
          <el-option label="更新" value="2"></el-option>
          <el-option label="插入" value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          class="bs-form-btn"
          size="small"
          type="primary"
          @click="onSearch"
          ><el-icon><Search /></el-icon>查 询</el-button
        >
        <ResetButton :queryParams="queryParams" />
        <el-button v-permission="['system::sqlAdd']" size="small" type="primary" @click="openModal({})"
          ><el-icon><Plus /></el-icon>新增
        </el-button>
      </el-form-item>
    </el-form>
    <!-- Search end -->

    <!-- Table start -->
    <div class="dataContainer">
      <el-table
        ref="tableRef"
        v-loading="tableLoading"
        :border="true"
        class="bs-table"
        :data="tableData"
        :header-cell-style="{ textAlign: 'center' }"
        :cell-style="{ textAlign: 'center' }"
        table-layout='auto'
      >
        <el-table-column type="index" label="序号" width="80" />
        <el-table-column prop="code" label="sql编号" width="180" />
        <el-table-column prop="type" label="sql类型" width="120">
          <template #default="scope">
            {{ getSqlType(scope.row.type) }}
          </template>
        </el-table-column>
        <el-table-column label="sql语句详情" width="380">
          <template #default="scope">
            <div class="sqlDetail">
              <el-popover
              effect="dark"
              placement="right"
              title=""
              trigger="hover"
              :width="400"
              :content="scope.row.sqlText"
              popper-class="define-popper"
              :teleported="true" 
            >
              <template #reference>
                <div > {{ scope.row.sqlText }} </div> 
              </template>
            </el-popover>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="descript" label="描述">
          <template #default="scope">
            <el-popover
              effect="dark"
              placement="right"
              title=""
              trigger="hover"
              :width="400"
              :content="scope.row.descript"
              popper-class="define-popper"
              :teleported="true" 
            >
              <template #reference>
                <div
                  style="
                    white-space: nowrap;
                    overflow: hidden;
                    text-overflow: ellipsis;
                  "
                >
                  {{ scope.row.descript }}
                </div>
              </template>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column prop="creatorName" label="创建人" width="120" />
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column prop="updatorName" label="修改人" width="120" />
        <el-table-column prop="updateTime" label="修改时间" />
        <el-table-column fixed="right" label="操作" width="280" v-permission="['system::sqlRun','system::userUpdate','system::sqlDelete']">
          <template #default="scope">
            <el-button size="small" link type="success" @click="openSqlRunModal(scope.row)" v-permission="['system::sqlRun']"><el-icon><CaretRight /></el-icon> 运行</el-button
            >
            <el-button size="small" link type="success" @click="openModal(scope.row)" v-permission="['system::userUpdate']" ><el-icon><Edit /></el-icon> 修改</el-button
            >
            <el-button size="small" link type="danger" @click="deleteSql(scope.row)" v-permission="['system::sqlDelete']" ><el-icon><Delete /></el-icon> 删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        small="small"
        disabled="disabled"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableTotal"
        @pagination="selectByPage"
      />
    </div>
    <AddOrUpdateSql ref="addOrUpdateSqlRef" @flushTable="onSearch" />
    <!-- sql试运行抽屉 -->
    <SqlRunWindowsVue ref="sqlRunRef" />
  </div>
</template>

<script>
import { selectByPage, deleteById } from "@/api/system/sql";
import { ElMessage, MessageBox } from "element-plus";
import AddOrUpdateSql from "./addOrUpdateSql.vue";
import SqlRunWindowsVue from './sqlRunWindows.vue'
export default {
  components: {
    AddOrUpdateSql,
    SqlRunWindowsVue,
  },
  data() {
    return {
      tableTotal: 0,
      tableLoading: false,
      tableData: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 500,
        code: "",
        type: "",
      },
    };
  },
  created() {
    this.selectByPage();
  },
  methods: {
    selectByPage() {
      this.tableLoading = true;
      selectByPage(this.queryParams)
        .then((res) => {
          this.tableLoading = false;
          if (res.code === 1) {
            this.tableData = res.data.records;
            this.tableTotal = res.data.total;
          }
        })
        .catch((error) => {
          this.tableLoading = false;
        });
    },
    // 搜索
    onSearch() {
      this.queryParams.pageNo = 1;
      this.selectByPage();
    },
    // 获取类型名称
    getSqlType(type) {
      if (type === 1) {
        return "查询";
      } else if (type === 2) {
        return "更新";
      } else if (type === 3) {
        return "插入";
      } else {
        return "--";
      }
    },
    openModal(row) {
      this.$refs.addOrUpdateSqlRef.openModal({ id: row.id });
    },
    deleteSql(row) {
      this.$confirm("请确认是否删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteById({ id: row.id }).then((res) => {
            if (res.code == 1) {
              this.$message.success("操作成功");
              this.selectByPage();
            }
          });
        })
        .catch(() => {});
    },
    // 打开sql运行窗口
    openSqlRunModal(row) {
      this.$nextTick(()=>{
        this.$refs.sqlRunRef.openRunModal(row.id)
      })
    }
  },
};
</script>

<style scoped>
.define-popper {
  background-color: aqua;
}
.sqlDetail {
  word-break: break-all;
  display: -webkit-box;
  -webkit-line-clamp: 4;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
</style>
